package com.vsked.ui;

import java.awt.Choice;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.sun.corba.se.impl.encoding.CodeSetConversion.BTCConverter;
import com.vsked.bean.ConnectionInfo;
import com.vsked.connection.ConnectionUtil;

public class SqlServerConnectUI extends JFrame implements ActionListener,ItemListener{
	JComboBox jcbServerType=new JComboBox();
	JTextField jtfDriverStr=new JTextField("com.microsoft.jdbc.sqlserver.SQLServerDriver");
	JTextField jtfServerAddr=new JTextField("localhost");
	JTextField jtfServerPort=new JTextField("1433");
	JTextField jtfDataBaseName=new JTextField("pubs");
	JTextField jtfServerUserName=new JTextField("sa");
	JTextField jtfServerUserPass=new JTextField("123456");
	
	JButton btTest=new JButton("TestConnection");
	JButton btSave=new JButton("SaveConnection");
	JButton btExit=new JButton("ExitSystem");
	
	public SqlServerConnectUI(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		
		int labelX=20;
		int labelY=10;
		int labelWidth=90;
		int labelHeight=20;
		
		int jtfX=labelX+80;
		int jtfY=labelY;
		int jtfWidth=370;
		int jtfHeight=labelHeight;
		
		JLabel labelServerType=new JLabel("DataBaseType:");
		labelServerType.setBounds(labelX, labelY, labelWidth, labelHeight);
		add(labelServerType);
		
        jcbServerType.addItem("MsSqlServer2000");
        jcbServerType.addItem("Oracle");
        jcbServerType.addItem("MySql");
        jcbServerType.addItemListener(this);
		jcbServerType.setBounds(jtfX, jtfY, jtfWidth, jtfHeight);
		add(jcbServerType);
		
		
		JLabel labelDriverStr=new JLabel("Driver:");
		labelDriverStr.setBounds(labelX, labelY+30, labelWidth, labelHeight);
		add(labelDriverStr);
		
		jtfDriverStr.setBounds(jtfX, jtfY+30, jtfWidth, jtfHeight);
		add(jtfDriverStr);
		
		JLabel labelServerAddr=new JLabel("Location:");
		labelServerAddr.setBounds(labelX, labelY+60, labelWidth, labelHeight);
		add(labelServerAddr);
		
		jtfServerAddr.setBounds(jtfX, jtfY+60, jtfWidth, jtfHeight);
		add(jtfServerAddr);
		
		
		JLabel labelServerPort=new JLabel("Port:");
		labelServerPort.setBounds(labelX, labelY+90, labelWidth, labelHeight);
		add(labelServerPort);
		
		jtfServerPort.setBounds(jtfX, jtfY+90, jtfWidth, jtfHeight);
		add(jtfServerPort);
		
		JLabel labelDataBaseName=new JLabel("DatabaseName:");
		labelDataBaseName.setBounds(labelX, labelY+120, labelWidth, labelHeight);
		add(labelDataBaseName);
		
		jtfDataBaseName.setBounds(jtfX, jtfY+120, jtfWidth, jtfHeight);
		add(jtfDataBaseName);
		
		JLabel labelServerUserName=new JLabel("User:");
		labelServerUserName.setBounds(labelX, labelY+150, labelWidth, labelHeight);
		add(labelServerUserName);
		
		jtfServerUserName.setBounds(jtfX, jtfY+150, jtfWidth, jtfHeight);
		add(jtfServerUserName);
		
		JLabel labelServerUserPass=new JLabel("Pass:");
		labelServerUserPass.setBounds(labelX, labelY+180, labelWidth, labelHeight);
		add(labelServerUserPass);
		
		jtfServerUserPass.setBounds(jtfX, jtfY+180, jtfWidth, jtfHeight);
		add(jtfServerUserPass);
		
		
		btTest.setBounds(jtfX-50,jtfY+220,100,30);
		btTest.addActionListener(this);
		add(btTest);
		
		btSave.setBounds(jtfX+100,jtfY+220,100,30);
		btSave.addActionListener(this);
		add(btSave);
		
		btExit.setBounds(jtfX+250,jtfY+220,100,30);
		btExit.addActionListener(this);
		add(btExit);
		
		
		
		setTitle("DatabaseConnection");
		setSize(500, 300);
		int cx=(Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth())/2;
		int cy=(Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight())/2;
		setLocation(cx, cy);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new SqlServerConnectUI();
	}

	/**
	 * event process
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btTest){
			try{
				ConnectionInfo ci=new ConnectionInfo();
				ci.setDbName(jtfDataBaseName.getText().trim());
				ci.setDbPort(Integer.parseInt(jtfServerPort.getText().trim()));
				ci.setDriverStr(jtfDriverStr.getText().trim());
				ci.setServerAddr(jtfServerAddr.getText().trim());
				ci.setUserName(jtfServerUserName.getText());
				ci.setUserPass(jtfServerUserPass.getText());
				System.out.println(jcbServerType.getSelectedItem().toString());
				if("mssqlserver2000".equals(jcbServerType.getSelectedItem().toString().toLowerCase())){
				    new ConnectionUtil().getSqlServer2000Connection(ci);
				}
				if("oracle".equals(jcbServerType.getSelectedItem().toString().toLowerCase())){
					new ConnectionUtil().getOracleConnection(ci);
				}
				if("mysql".equals(jcbServerType.getSelectedItem().toString().toLowerCase())){
					new ConnectionUtil().getMySqlConnection(ci);
				}
				JOptionPane.showConfirmDialog(null, "Connect Success!");
			}catch(Exception ex){
				ex.printStackTrace();
				JOptionPane.showConfirmDialog(null, "Connect Fail!"+ex.getMessage());
			}
		}
		if(e.getSource()==btSave){
			
		}
		if(e.getSource()==btExit){
			if(JOptionPane.showConfirmDialog(null, "Are you sure exist?")==0){
			  dispose();
			}
		}
		
	}//end actionPerformed

	/**
	 * 
	 */
	public void itemStateChanged(ItemEvent e) {
		String typeStr=e.getItem().toString().toLowerCase();
		if("mssqlserver2000".equals(typeStr)){
			ConnectionInfo ci=ConnectionInfo.getDefaultSqlServer2000Info();
			jtfDriverStr.setText(ci.getDriverStr());
			jtfServerAddr.setText(ci.getServerAddr());
			jtfServerPort.setText(ci.getDbPort()+"");
			jtfServerUserName.setText(ci.getUserName());
			jtfServerUserPass.setText(ci.getUserPass());
			jtfDataBaseName.setText(ci.getDbName());
		}
        if("oracle".equals(typeStr)){
        	ConnectionInfo ci=ConnectionInfo.getDefaultOracleInfo();        	
			jtfDriverStr.setText(ci.getDriverStr());
			jtfServerAddr.setText(ci.getServerAddr());
			jtfServerPort.setText(ci.getDbPort()+"");
			jtfServerUserName.setText(ci.getUserName());
			jtfServerUserPass.setText(ci.getUserPass());
			jtfDataBaseName.setText(ci.getDbName());
		}
        if("mysql".equals(typeStr)){
        	ConnectionInfo ci=ConnectionInfo.getDefaultMySqlInfo();
			jtfDriverStr.setText(ci.getDriverStr());
			jtfServerAddr.setText(ci.getServerAddr());
			jtfServerPort.setText(ci.getDbPort()+"");
			jtfServerUserName.setText(ci.getUserName());
			jtfServerUserPass.setText(ci.getUserPass());  
			jtfDataBaseName.setText(ci.getDbName());
		}
	}//end itemStateChaned

}
